/*
Copyright (C) 2010 Martin Zuber, zuber@centrum.cz

This application is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation; either version 2.1 of the
License, or (at your option) any later version.

This application is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with This application; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

#include "dsettings.h"
#include "ui_dsettings.h"

#include <QSettings>
#include <QDebug>
#include <QRegExp>
#include <QMessageBox>

DSettings::DSettings(QWidget *parent) :
        QDialog(parent),
        ui(new Ui::DSettings)
{
    ui->setupUi(this);

    initializeUi();

    connect(ui->lstPresets, SIGNAL(currentTextChanged(QString)), this, SLOT(on_lstPresets_changed(QString)));
}

DSettings::~DSettings()
{
    delete ui;
}

void DSettings::initializeUi()
{
    // for mobile devices, it should be maximized
    setWindowState(Qt::WindowMaximized);

    // load settings
    QSettings settings;
    QStringList keys = settings.allKeys();

    if(keys.size() == 0)
    {
        qDebug() << "No presets found, the defaults will be created";

        settings.setValue("PresetBlack Tea", QTime::fromString("00:03:00", "HH:mm:ss"));
        settings.setValue("PresetFruit Tea", QTime::fromString("00:03:00", "HH:mm:ss"));
        settings.setValue("PresetHerbal Tea", QTime::fromString("00:03:00", "HH:mm:ss"));
        settings.setValue("PresetEggs", QTime::fromString("00:10:00", "HH:mm:ss"));
        settings.setValue ("PresetDumplings", QTime::fromString("00:22:00", "HH:mm:ss"));

        qDebug() << "Defaults created";

        // reload data
        keys = settings.allKeys();

        qDebug() << "The setting were reloaded, number of keys: " << keys.size();
    }


    // insert settings into list
    foreach(QString key, keys)
    {
        if(key.startsWith("Preset"))
        {
            qDebug() << "Loading data for preset: " << key;
            QTime presetValue = settings.value(key).toTime();

            QRegExp regex;
            ui->lstPresets->addItem(key.replace(QString("Preset"), QString("")));

            qDebug() << "... value is: " << presetValue;
        }
    }
}

void DSettings::on_lstPresets_changed(QString newText)
{
    QSettings settings;
    qDebug() << "New text: " << newText;
    QTime presetTime = settings.value(QString("Preset").append(newText)).toTime();
    qDebug() << "New text: " << presetTime;

    if(presetTime.isNull())
    {
        QMessageBox::warning(this,
                             tr("No data"),
                             tr("The requested entry has no data attached!<br/><br/>It seems that this entry is invalid."),
                             QMessageBox::Ok);
        return;
    }

    ui->lePresetName->setText(newText);
    ui->tePresetTimer->setTime(presetTime);
}
